<template>
  <div class="Login">
    <div class="Login-warp">
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="username"
          type="number"
          placeholder="请输入手机号"
          :rules="[{ required: true }]"
        />
        <van-field
          v-model="sms"
          center
          clearable
          placeholder="请输入短信验证码"
        >
          <template #button>
            <van-button
              size="small"
              type="primary"
              native-type="button"
              color="#31a5fc"
              @click="getcode"
              >发送验证码</van-button
            >
          </template>
        </van-field>
        <van-button
          color="#31a5fc"
          square
          block
          type="info"
          native-type="submit"
          class="btn"
        >
          登录
        </van-button>
      </van-form>
      <van-divider style="margin-top:100px">其他登录方式</van-divider>
      <ul class="way">
        <li>
          <img
            src="https://image.benlailife.com/passport/login_alipay.png"
            alt=""
          />
        </li>
        <li>
          <img
            src="https://image.benlailife.com/passport/login_qq.png"
            alt=""
          />
        </li>
        <li>
          <img
            src="https://image.benlailife.com/passport/login_wb.png"
            alt=""
          />
        </li>
      </ul>
      <div class="message">
        <span>未注册的手机号验证后将自动创建地摊账号</span><br />
        <span>登录即代表您同意《地摊商城网用户协议》《隐私政策》</span>
      </div>
      <van-dialog v-model="show" title="手机号未输入或输入错误"> </van-dialog>
      <van-dialog v-model="bool" title="登陆失败，验证码输入错误"></van-dialog>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import Vue from "vue";
import { Form, Field, Button, Divider, Dialog } from "vant";

Vue.use(Form);
Vue.use(Field);
Vue.use(Button);
Vue.use(Divider);
Vue.use(Dialog);

export default {
  name: "Login",
  data() {
    return {
      username: "",
      sms: "",
      show: false,
      bool: false
    };
  },
  methods: {
    ...mapMutations(["changeIsLogin"]),
    onSubmit() {
      if (this.username.length !== 11) {
        this.show = true;
      } else {
        // console.log("in");
        this.show = false;
        this.$http.getRequest(this.username, this.sms).then(resp => {
          // console.log(resp);
          if (resp.data.code === 200) {
            // 修改登录状态
            this.changeIsLogin({ status: true });
            // 手机号
            sessionStorage.mobile = this.username;
            this.$router.push({ name: "Mine" });
          } else {
            this.bool = true;
          }
        });
      }
    },
    //获取验证码
    getcode() {
      // console.log(this.username);
      if (this.username.length !== 11) {
        this.show = true;
      } else {
        this.$http.getCode(this.username).then(resp => {
          // console.log(res)
          if (resp.code === 200) {
            // console.log(resp);
          }
        });
      }
    }
  }
};
</script>

<style lang="scss">
.Login-warp {
  height: 100%;
  overflow: hidden;
  .van-form {
    margin-top: 80px;
    border-top: 1px solid #31a5fc;
  }
}
.message {
  width: 80%;
  margin: 0px auto;
  color: #bbb;
  font-size: 12px;
  text-align: center;
  line-height: 20px;
  margin-top: 80px;
}
.way {
  width: 60%;
  margin: 40px auto 74px;
  display: flex;
  justify-content: space-between;
  justify-items: right;
  li {
    width: 30%;
    text-align: center;
    img {
      width: 36px;
      height: 36px;
    }
  }
}
</style>
